/*
 * @Author: xiangru.xiao xiangru.xiao@mthreads.com
 * @Date: 2023-05-25 09:32:54
 * @LastEditors: xiangru.xiao xiangru.xiao@mthreads.com
 * @LastEditTime: 2023-05-25 16:49:43
 * Copyright (c) 2023 by Moore Threads, All Rights Reserved. 
 */

#include <string.h>
#include <vector>
#include "objectfactory.h"
#include "node.h"
#include "stopwatch.h"
#include "clcheck.h"
#include "cldeviceinfo.h"
#include "clevent.h"
#include "clBLAS.h"
#include "math.h"

#define EPS 1e-6
#define TEST_ITERS 4
struct params
{
    int length;
};

class GEMM_test : public Node
{
public:
    GEMM_test()
    {
        nodeUint = "GFLOPS";
    }
    std::string name() override
    {
        return "gemm-test";
    }
    std::string describe() override
    {
        return "矩阵乘法计算";
    }
    Node::NodeType  nodeType() override
    {
        return NodeTP_DataSpeed;
    }
protected:
    bool startup() override;
    bool perform() override;
    void shutdown() override;
private:
    std::vector<params> paramList() const;
    bool testResult(float* h_a, float* h_b, float* h_c, 
                cl_mem d_a, cl_mem d_b, cl_mem d_c, int length);
private:
    struct cl_handle_t {
        cl_device_id        deviceid;
        cl_context          context;
        cl_command_queue    queue;
    } handle;
};

bool GEMM_test::startup()
{
    memset(&handle, 0, sizeof(cl_handle_t));

    handle.deviceid = deviceinfo->deviceID();
    handle.context = clCreateContext(NULL, 1,  &handle.deviceid, NULL, NULL, NULL);
    handle.queue = clCreateCommandQueue(handle.context, handle.deviceid,
                                        CL_QUEUE_PROFILING_ENABLE,
                                        NULL);
    return true;
}

bool GEMM_test::perform()
{
    /* Setup clblas. */
    CHECK_CL_ERROR(clblasSetup());
    float totalValue = 0.0;
    stopwatch timer;
    cl_int err;
    std::vector<params> ls = paramList();
    char* strNum = std::getenv("GEMM_TEST_LEVEL");
    int caseNum = ls.size();
    if(strNum)
    {
        caseNum = std::min(caseNum, atoi(strNum));
    }
    for (int i = 0; i < caseNum; ++i) {
        int length = ls[i].length;
        float alpha = 1.1, beta = 0.9;
        const int real_bytes = sizeof(float) * length * length;
        float *h_a = (float*)malloc(real_bytes);
        float *h_b = (float*)malloc(real_bytes);
        float *h_c = (float*)malloc(real_bytes);
        cl_mem d_a = clCreateBuffer(handle.context, CL_MEM_READ_WRITE, real_bytes, NULL, &err);
        CHECK_CL_ERROR(err);
        cl_mem d_b = clCreateBuffer(handle.context, CL_MEM_READ_WRITE, real_bytes, NULL, &err);
        CHECK_CL_ERROR(err);
        cl_mem d_c = clCreateBuffer(handle.context, CL_MEM_READ_WRITE, real_bytes, NULL, &err);
        CHECK_CL_ERROR(err);
        if(testResult(h_a, h_b, h_c, d_a, d_b, d_c, length))
        { //result correct and then do profile
            timer.start();
            for (int j = 0; j < TEST_ITERS; ++j) {
                  CHECK_CL_ERROR(clblasSgemm(clblasRowMajor, clblasNoTrans, clblasTrans, length, length, length,
                                              alpha, d_a, 0, length,
                                              d_b, 0, length, beta,
                                              d_c, 0, length,
                                              1, &(handle.queue), 0, NULL, NULL));
                  CHECK_CL_ERROR(clFinish(handle.queue));
            }
            int totalTime  = static_cast<int>(timer.usElapsed());
            CHECK_CL_ERROR(clReleaseMemObject(d_a));
            CHECK_CL_ERROR(clReleaseMemObject(d_b));
            CHECK_CL_ERROR(clReleaseMemObject(d_c));
            free(h_a);
            free(h_b);
            free(h_c);
            totalValue += 2 * float(length) * float(length) * float(length)
                          * TEST_ITERS / totalTime / 1e3f;
        }
        else
        { //get wrong result
            nodeValue = 0.0;
            CHECK_CL_ERROR(clReleaseMemObject(d_a));
            CHECK_CL_ERROR(clReleaseMemObject(d_b));
            CHECK_CL_ERROR(clReleaseMemObject(d_c));
            free(h_a);
            free(h_b);
            free(h_c);
            /* Finalize work with clblas. */
            clblasTeardown();
            return false;
        }
        
    }
    nodeValue = totalValue;
    /* Finalize work with clblas. */
    clblasTeardown();
    return true;
}

void GEMM_test::shutdown()
{
    if (handle.queue) {
        CHECK_CL_ERROR(clReleaseCommandQueue(handle.queue));
        handle.queue = nullptr;
    }
    if (handle.context) {
        CHECK_CL_ERROR(clReleaseContext(handle.context));
        handle.context = nullptr;
    }
}

std::vector<params> GEMM_test::paramList() const
{
    std::vector<params> list;
    params params_[] = {512, 1024, 2048, 4096, 8192};
    const size_t nSizes =  sizeof(params_) / sizeof (params);
    for (int i = 0; i < nSizes; ++i) {
        list.push_back(params_[i]);
    }
    return list;
}

bool GEMM_test::testResult(float* h_a, float* h_b, float* h_c, 
                cl_mem d_a, cl_mem d_b, cl_mem d_c, int length)
{
    int N = length * length;
    float alpha = 1.25, beta = 0.5;
    float* h_out = (float*)malloc(sizeof(float) * N);
    for(int i = 0; i < N; i++)
    {
        h_a[i] = alpha;
        h_b[i] = beta;
        h_c[i] = float(i%16);
    }
    CHECK_CL_ERROR(clEnqueueWriteBuffer(handle.queue, d_a, CL_TRUE, 0, 
                                sizeof(float) * N, h_a, 0, NULL, NULL));
    CHECK_CL_ERROR(clEnqueueWriteBuffer(handle.queue, d_b, CL_TRUE, 0, 
                                sizeof(float) * N, h_b, 0, NULL, NULL));
    CHECK_CL_ERROR(clEnqueueWriteBuffer(handle.queue, d_c, CL_TRUE, 0, 
                                sizeof(float) * N, h_c, 0, NULL, NULL));
    CHECK_CL_ERROR(clblasSgemm(clblasRowMajor, clblasNoTrans, clblasTrans, length, length, length,
                                              alpha, d_a, 0, length,
                                              d_b, 0, length, beta,
                                              d_c, 0, length,
                                              1, &(handle.queue), 0, NULL, NULL));
    CHECK_CL_ERROR(clFinish(handle.queue));
    CHECK_CL_ERROR(clEnqueueReadBuffer(handle.queue, d_c, CL_TRUE, 0, 
                               sizeof(float) * N, h_out, 0, NULL, NULL));

    double maxv = alpha * beta * length * alpha + beta * 15;
    double nrmse = 0;  // normalized root mean square error
    for (int i = 0; i < N; i++) {
      double dr = alpha * beta * length * alpha + beta * h_c[i] - h_out[i];
      nrmse += (dr * dr);
    }
    nrmse /= (double)(N);
    nrmse = sqrt(nrmse);
    nrmse /= maxv;
    free(h_out);
    return nrmse < EPS;
}

REGISTER_OBJECT(Node, GEMM_test)